package com.wy.component.database;

import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

public class DBUtil {
	
	private static String driverClassName;
	private static String url;
	private static String username;
	private static String passwd;
	
	private static Connection conn=null;
	private static PreparedStatement pst=null;
	private static ResultSet res=null;
	
	static{
		 Properties prop = new Properties();
		 InputStream in=Object.class.getResourceAsStream("/jdbc.properties");
		 
		 try {
			prop.load(in);
			driverClassName=prop.getProperty("driverClassName").trim();
			url=prop.getProperty("url").trim();
			username=prop.getProperty("username").trim();
			passwd=prop.getProperty("password").trim();			
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public static Connection getConnection(){
		try {
			Class.forName(driverClassName);
			conn=DriverManager.getConnection(url, username, passwd);
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        return conn;		
	}
	
	
	public static void closeConnection(Connection conn,Statement stat){
		try {
			stat.close();
			conn.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	
     public static void main(String[] args) {
    	System.out.println(getConnection());
	}
	
	
}
